package cn.myra.testbefore;

public class TestString {

	public static void main(String[] args) {

		// 字符串拆分和拼接
		String sourcestr = "122 2323 8989";
		String targetstr;
		// 拆分字符串
		String[] strs = sourcestr.split("\\s");
		//
		StringBuilder sBuilder = new StringBuilder();
		// 遍历字符串数组 ，连接字符串
		for (String str : strs) {
			sBuilder.append(str);
		}

		targetstr = sBuilder.toString();
		System.out.println("targetstr = sBuilder.toString()=" + targetstr);
		System.out.println("sBuilder" + sBuilder);

		// 切割子串
		String str11 = targetstr.substring(0, 3);
		String str12 = targetstr.substring(3, 7);
		String str13 = targetstr.substring(7);
		// 子串后附加空格并连接
		String str14 = str11 + " " + str12 + " " + str13;
		System.out.println("String str14 = str11+\" \"+str12+\" \"+str13=" + str14);
		StringBuilder strbd = new StringBuilder();
		strbd.append(str11+" "+str12+" "+str13);
		StringBuffer strbd1 = new StringBuffer();
		strbd1.append(str11+" "+str12+" "+str13);
		

		// StringBuffer插入空格
		StringBuffer sBuffer1 = new StringBuffer(targetstr);
		sBuffer1.insert(3, " ");
		sBuffer1.insert(7, " ");
		System.out.println("sBuffer1=" + sBuffer1);

		// 字符串 包装类 和基本类型的转化
		int iVal1 = 23;

		// 自动转换
		Integer iInteger1 = iVal1;
		Integer iInteger4 = iVal1;
		// 构造方法转换
		Integer iInteger2 = new Integer(iVal1);
		// valueOf
		Integer iInteger3 = Integer.valueOf(iVal1);

		// 判断对象相等于是否为同一个对象
		System.out.println("iInteger1.equals(iInteger2)" + (iInteger1.equals(iInteger2)));// true
		System.out.println("iInteger1==iInteger2" + (iInteger1 == iInteger2));// false

		System.out.println("iInteger1.equals(iInteger4)" + (iInteger1.equals(iInteger4)));// true
		System.out.println("iInteger1==iInteger4" + (iInteger1 == iInteger4));// true

		// 自动转换
		String str1 = iVal1 + "";
		// valueOf
		String str2 = String.valueOf(iVal1);
		// 构造方法
		String str3 = new String("hello");
		// valueOf
		String str4 = String.valueOf(iInteger2);
		// toString
		String str5 = iInteger1.toString();
		//
		String str6 = iInteger1.toString(333);
		//
		String str7 = Integer.toString(iInteger1);
		System.out.println(str6 + " " + str7);

		// parseInt
		int iVal2 = Integer.parseInt(str7);
		// valueOf
		int iVal3 = Integer.valueOf(str2).intValue();
		// valueOf
		Integer iInteger5 = Integer.valueOf(str1);

	}

}
